/*
 * CommunicationADP.cpp
 *
 *  
 */

#include "CommunicationADP.h"
#include "../common/IPCUids.h"
#include "../capa_servicios_distribuidos/QueueLauncher.h"
#include "../channel_common/agent_address_service/AgentAddressService.h"

using namespace std;

namespace middleware {

const Uid CommunicationADP::UID = "adpRecepcion";

CommunicationADP::CommunicationADP(std::string hostName) :
		colaRecepcion(ipc::MessageQueue<ProductoCantidad>::get(IPCUids::getFile(UID)+hostName,IPCUids::getKey(UID))) {

	mtypeRecepADP = channel::AgentAddressService::resolve(UID);
}

void CommunicationADP::create(ADPCommunicationType type, std::string hostName){
	int mtypeRecepADP = 0;

	if (type == ADP) {
		mtypeRecepADP = channel::AgentAddressService::registerName(UID);
	}

	ipc::MessageQueue<ProductoCantidad>::create(IPCUids::getFile(UID)+hostName,IPCUids::getKey(UID));
}

void CommunicationADP::destroy(ADPCommunicationType type, std::string hostName){
	ipc::MessageQueue<ProductoCantidad>::destroy(IPCUids::getFile(UID)+hostName,IPCUids::getKey(UID));
}

void CommunicationADP::producir(ProductoCantidad& productos) {
	colaRecepcion.put(mtypeRecepADP, productos);
}

ProductoCantidad CommunicationADP::obtenerAProducir() {
	return colaRecepcion.get(mtypeRecepADP);
}

CommunicationADP::~CommunicationADP() {}

} /* namespace middleware */
